<?php include("includes/session.php"); ?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo $lang['PAGE_TITLE_VIEW_PROJECT']; ?></title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=<?php echo GOOGLE_KEY; ?>"
	type="text/javascript">
</script>
<script type="text/javascript">
function initialize() {
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(0, 0), 0);
	map.setUIToDefault();
	map.setMapType(G_SATELLITE_MAP);
	var points = Array(<?=$database->getAllMemberLocationsWithNames($_GET['id']); ?>);
	for (var i = 0; i < points.length; i ++)
	{
		var marker = new GMarker(new GLatLng(points[i][0], points[i][1]));
	//document.getElementById("map_name").innerHTML = "lol";
		var self = this;
		GEvent.addListener(marker,"mouseover",
			new Function("document.getElementById(\"map_name\").innerHTML = \""+points[i][2]+"\";"));
		GEvent.addListener(marker,"click",
			new Function("window.location = \"profile.php?user="+points[i][2]+"\";"));
		map.addOverlay(marker);
	}
	//echo "map.addOverlay(new GMarker(new GLatLng($location)));\n";
	";";";";//GEvent.addListener(map,"click", function(overlay, latlng) {
  }
}
</script>
<?php include("header.php"); ?>
	<center>
	<table cellpadding="0" cellspacing="0" width="95%">
	<tr>
		<td align="left">
			<?php 
			if(!$session->logged_in && !isset($_GET['id']))
			{
				echo "<center><h3>You must be logged in to view your project.</h3></center>";
			}
			else
			{
				unset($project);
				if(!isset($_GET['id']))
				{
					echo "<br /><center>No Project Found</center>";
				}
				$project = $database->getProject($_GET['id']);
				if($project=="")
				{
					echo "<br /><center>No Project Found</center>";
				}
				else
				{
			?>
			<table cellpadding="10" cellspacing="0" width="100%">
			<tr>
				<td></td>
				<td></td>
			</tr>
			<tr>
				<td valign="top">
					<!-- Project Image -->
					<table cellpadding="0" cellspacing="0" width="232px">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
						<center><img width="200" border="1" src="<?php if($project['logoPic']==''){ echo DEFAULT_PROJECT_PIC; }else{ echo $project['logoPic']; } ?>" /></center>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					<br />
					<!-- Join / Leave -->
					<?php 
					$result = $database->query("SELECT * FROM ".TBL_PROJECT_MEMBERS." WHERE username='".$session->username."' AND projectID='".$project['id']."'");
					$num_member = mysql_num_rows($result);
					if($project['isPrivate']==0 || $num_member>0)
					{
					?>
					<table cellpadding="0" cellspacing="0" width="232px">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
						<?php 
						if(!$session->logged_in)
						{
							echo "<center><a href=\"index.php\">Log In to Join!</a></center>";
						}
						else if(isset($_SESSION['joinProjectSuccess']))
						{
							if($_SESSION['joinProjectSuccess'])
							{
								echo "<center>Successfully Joined</center>";
							}
							else
							{
								echo "<center>Joined Failed</center>";
							}
							unset($_SESSION['joinProjectSuccess']);
						}
						else if(isset($_SESSION['leaveProjectSuccess']))
						{
							if($_SESSION['leaveProjectSuccess'])
							{
								echo "<center>Successfully Left Project</center>";
							}
							else
							{
								echo "<center>Leave Project Failed</center>";
							}
							unset($_SESSION['leaveProjectSuccess']);
						}
						else if(isset($_SESSION['addlikesuccessful']))
						{
							if($_SESSION['addlikesuccessful'])
							{
								echo "<center>Like Project Successful</center>";
							}
							else
							{
								echo "<center>You Already Like This Project</center>";
							}
							unset($_SESSION['addlikesuccessful']);
						}
						else
						{
							echo "<center>";
							if($num_member>0)
							{
								echo "<a href=\"includes/process.php?leaveProject=".$project['id']."&user=".$session->username."\" title=\"Leave the Project?\">Leave</a>";
							}
							else if($project['isPrivate']==0)
							{
								echo "<a href=\"includes/process.php?joinProject=".$project['id']."&user=".$session->username."\" title=\"Join the Project?\">Join</a>";
							}
							
							echo " - <a href=\"includes/process.php?addLike=".$project['id']."&type=0\" title=\"Like This Project?\">Likes ".$project['likes']."</a>";
							echo "</center>";
						}
						?>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					<br />
					<?php
					}
					
					$resul = $database->query("SELECT * FROM ".TBL_PROJECT_MEMBERS." WHERE projectID='".$project['id']."' AND username='".$session->username."'");
					$projectMember = mysql_fetch_array($resul);
					if($project['creator']==$session->username || $session->userlevel>=MANAGER_LEVEL || $projectMember['userlevel']>=PROJECT_MANAGER)
					{
					?>
					<!-- Project Creator Panel -->
					<table cellpadding="0" cellspacing="0" width="232px">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
							<table cellpadding="3" cellspacing="0" width="100%">
							<tr>
								<td><a href="editProject.php?edit=<?php echo $project['id']; ?>"><?php echo $lang['VIEW_PROJECT_EDIT_PROJECT']; ?></a></td>
							</tr>
							<tr>
								<td><a href="editProjectResources.php?id=<?php echo $project['id']; ?>"><?php echo $lang['VIEW_PROJECT_EDIT_NEEDED_RESOURCES']; ?></a></td>
							</tr>
							<tr>
								<td><a href="editProjectNews.php?id=<?php echo $project['id']; ?>"><?php echo $lang['VIEW_PROJECT_EDIT_NEWS']; ?></a></td>
							</tr>
							<?php
							if($projectMember['userlevel']>=PROJECT_OWNER)
							{
							?>
							<tr>
								<td><a href="editProjectMembers.php?id=<?php echo $project['id']; ?>"><?php echo $lang['VIEW_PROJECT_EDIT_MEMBERS']; ?></a></td>
							</tr>
							<?php
							}
							if($session->userlevel>=MANAGER_LEVEL)
							{
							?>
							<tr>
								<td><a href="#" onClick="if(disp_confirm_delete_project()){buttonClick('includes/process.php?deleteProject=<?php echo $project['id']; ?>');}" class="button"><?php echo $lang['VIEW_PROJECT_DELETE_PROJECT']; ?></a></td>
							</tr>
							<?php
							}
							?>
							</table>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					<br />
					<?php
					}
					if($project['link']!='' || $project['email']!='' || $project['moneyNeeded']!='')
					{
					?>
					<!-- Project Details -->
					<table cellpadding="0" cellspacing="0" width="232px">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
							<div style="width:202px;overflow:auto;">
							<table cellpadding="3" cellspacing="0" width="100%">
							<tr>
								<td align="left"><b><?php echo $lang['VIEW_PROJECT_PROJECT_MANAGER']; ?></b></td>
							</tr>
							<tr>
								<td align="right"><font size="-2"><a href="profile.php?user=<?php echo $project['creator']; ?>" title="View Profile"><?php echo $project['creator']; ?></a></font></td>
							</tr>
							<?php if($project['link']!='')
							{
							?>
							<tr>
								<td align="left"><b><?php echo $lang['VIEW_PROJECT_WEBSITE']; ?></b></td>
							</tr>
							<tr>
								<td align="right"><font size="-2"><a href="<?php echo $project['link']; ?>" title="Visit Site" target="_blank"><?php echo $project['link']; ?></a></font></td>
							</tr>
							<?php
							}
							if($project['email']!='')
							{
							?>
							<tr>
								<td align="left"><b>E-mail:</b></td>
							</tr>
							<tr>
								<td align="right"><font size="-2"><?php echo $project['email']; ?></font></td>
							</tr>
							<?php
							}
							if($project['moneyNeeded']!='')
							{
							?>
							<tr>
								<td align="left"><b><?php echo $lang['PROJECT_MANAGER_DONATION']; ?></b></td>
							</tr>
							<tr>
								<td align="right"><font size="-2"><?php echo $project['moneyNeeded']; ?></font></td>
							</tr>
							<?php
							}
							?>
							</table>
						</div>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					<br />
					<?php
					}
					?>
					<!-- Project Members -->
					<table cellpadding="0" cellspacing="0" width="232px">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
							<?php displayProjectMembers($project['id']); ?>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					
				</td>
				<td valign="top" align="left" width="100%">
					<!-- Project Name -->
					<center>
					<table cellpadding="0" cellspacing="0" width="100%">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
						<center><font size="+2"><b><?php echo $project['name']; ?></b></font></center>
						<center><font color="#6c0000">
						<?php
						if($project['isPrivate']==0)
						{
							echo "Public";
						}
						else
						{
							echo "Private";
						}
						?>
						 Project</font> - <font color="#196c00">Started on <?php echo date("F j, Y",$project['timestamp']); ?></font>
						</center>
						<br />
						<center><?php echo $project['description']; ?></center>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					</center>
					<br />
					<center>
					<table cellpadding="0" cellspacing="0">
					<tr>
					<?php
					if($project['details']!='')
					{
					?>
					<td valign="top">
						<!-- Project Details -->
						<center>
						<table cellpadding="0" cellspacing="0">
						<tr>
							<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
						</tr>
						<tr bgcolor="#dadada">
							<td></td><td>
							<div style="overflow:auto;width:260px;height:250px;">
							<?php echo $project['details']; ?>
							</div>
							</td><td></td>
						</tr>
						<tr>
							<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
						</tr>
						</table>
						</center>
					</td>
					<td width="15px"></td>
					<?php
					}
					?>
					<td valign="top">
						<!-- Google Map -->
						<center>
						<table cellpadding="0" cellspacing="0">
						<tr>
							<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
						</tr>
						<tr bgcolor="#dadada">
							<td></td><td>
							<center>
							<table cellpadding="3" cellspacing="0" width="230px">
							<tr>
								<td>
								<div id="map_canvas" style="width: 300px; height: 220px;"></div>
								</td>
							</tr>
							<tr>
								<td>
								<Center><b><div id="map_name"></div></b></Center>
								</td>
							</tr>
							</table>
							</center>
							</td><td></td>
						</tr>
						<tr>
							<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
						</tr>
						</table>
						</center>
					</td>
					</tr>
					</table>
					</center>
					<br />
					<!-- Project Resources -->
					<center>
					<table cellpadding="0" cellspacing="0" width="100%">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
						<center><font size="+1"><b>Needed Resources</b></font></center>
						<center><?php displayProjectResources($project['id']); ?></center>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					</center>
					<br />
					<!-- Project News -->
					<center>
					<table cellpadding="0" cellspacing="0" width="100%">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
						<center><font size="+1"><b>Project News</b></font></center>
						<br />
						<center><?php displayProjectNews($project['id'],5); ?></center>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					</center>
					<br />
					<!-- Application Form -->
					<?php if($project['allowApply']==1)
					{
					?>
					<center>
					<table cellpadding="0" cellspacing="0" width="100%">
					<tr>
						<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
					</tr>
					<tr bgcolor="#dadada">
						<td></td><td>
						<center><font size="+1"><b>Apply</b></font></center>
						<center>
						<form action="includes/process.php" method="post">
						<input type="hidden" name="subApplication" value="<?php echo $project['id']; ?>" />
						<input type="hidden" name="to" value="<?php echo $project['creator']; ?>" />
						<input type="hidden" name="from" value="<?php echo $session->username; ?>" />
						<input type="hidden" name="subject" value="<?php echo $project['name']; ?>" />
						<input type="hidden" name="email" value="<?php echo $project['email']; ?>" />
						<input type="hidden" name="notifyType" value="<?php echo $project['applyNotify']; ?>" />
						<table cellpadding="5" cellspacing="0">
						<tr>
							<td>
							<center><textarea name="message" cols="60" rows="20" ><?php echo $project['application']; ?></textarea></center>
							</td>
						</tr>
						<tr>
							<td align="right">
							<?php
							if($session->logged_in)
							{
							?>
								<input type="submit" value="Submit Application"/>
							<?php
							}
							else
							{
								echo "<a href=\"index.php\">Log In to Apply!</a>";
							}
							?>
							</td>
						</tr>
						</table>
						</form>
						</center>
						</td><td></td>
					</tr>
					<tr>
						<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
					</tr>
					</table>
					</center>
					<?php
					}
					?>
				</td>
			</tr>
			</table>
			<?php 
				}
			}
			?>
		</td>
	</tr>
	</table>
	</center>
<?php include("footer.php"); ?>